package com.iplant.base.utils.sql;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
//@MapperScan(basePackages = "com.demo.mapper.mickey", sqlSessionTemplateRef = "mickeySqlSessionTemplate")
public class OracleDsConfig {

	@Primary
	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.druid.oracle")
	public DataSource OracleDataSource() {
		return DataSourceBuilder.create().build();
	}

	@Primary
	@Bean
	public SqlSessionFactory OracleSessionFactory(@Qualifier("OracleDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean.getObject();
	}

	@Primary
	@Bean(name = "OracleTransactionManager")
	public DataSourceTransactionManager OracleTransactionManager(@Qualifier("OracleDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Primary
	@Bean
	public SqlSessionTemplate OracleSessionTemplate(
			@Qualifier("OracleSessionFactory") SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
